package com.itrh.auth.controller;

import com.itrh.auth.dto.RoleDTO;
import com.itrh.auth.service.IRoleService;
import com.itrh.auth.domain.Role;
import com.itrh.auth.query.RoleQuery;
import com.itrh.base.annotation.AuthPermission;
import org.springframework.web.bind.annotation.*;
import com.itrh.org.util.Result;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/role")
@AuthPermission(name = "角色管理")
public class RoleController {
    @Resource
    public IRoleService roleService;

    @PutMapping
    @AuthPermission(name = "添加/修改角色")
    public Result addOrUpdate(@RequestBody Role role) {
        System.out.println("role:" + role);
        return roleService.saveOrUpdate(role);
    }

    @DeleteMapping(value = "/{id}")
    @AuthPermission(name = "删除角色")
    public Result delete(@PathVariable("id") Long id) {
        return roleService.deleteById(id);
    }

    // 获取用户
    @GetMapping("/{id}")
    @AuthPermission(name = "获取角色")
    public Result get(@PathVariable("id") Long id) {
        return roleService.selectById(id);
    }

    @GetMapping
    @AuthPermission(name = "获取角色列表")
    public Result list() {
        return roleService.selectAll();
    }

    @PostMapping
    @AuthPermission(name = "条件分页获取角色列表")
    public Result pageList(@RequestBody RoleQuery query) {
        return roleService.pageList(query);
    }

    @PatchMapping
    @AuthPermission(name = "批量删除角色")
    public Result batchRemove(@RequestBody List<Long> ids) {
        return roleService.batchRemove(ids);
    }

    @PutMapping("/saveOrUpdate")
    @AuthPermission(name = "添加/修改角色")
    public Result saveOrUpdate(@RequestBody RoleDTO dto) {
        return roleService.addOrUpdate(dto);
    }

    @GetMapping("/permission/{id}")
    public Result getPermissionIds(@PathVariable Long id) {
        return roleService.getPermissionIds(id);
    }

    @GetMapping("/menu/{id}")
    public Result getMenuIds(@PathVariable Long id) {
        return roleService.getMenuByRoleId(id);
    }
}
